package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;
    @Override
    public void importOrderSettings(List<OrderSetting> orderSettings) {
        //注意点:如果前期上传过模板,已经有了相关日期,则不需要新增而是选择更新
        for (OrderSetting orderSetting : orderSettings) {

            //检查当前日期在是否在数据库中设置过
            Integer count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
            if(count>0){
                //已经存在设置
                orderSettingMapper.updateNumberByOrderDate(orderSetting);
            }else{
                orderSettingMapper.add(orderSetting);
            }

        }

    }
    /**
     * 日历展示可预约数据
     * @param date
     * @return
     */
    /*@Override
    public List<Map> findOrderSettingByMonth(String date) {

        //定义查询的每个月的起始值
        String dateBegin = date+"-01";
        //定义查询的每个月的结束值
        String dateEnd = date+"-31";

        //通过日期查询指定月份的可预约数据
        List<OrderSetting> orderSettings = orderSettingMapper.findOrderSettingByMonth(dateBegin,dateEnd);

        //定义一个List，用来存放map
        List<Map> list = new ArrayList<>();

        //遍历每一个OrderSetting,将其重新封装为一个个的map，再将map重新封装到List中
        for (OrderSetting orderSetting : orderSettings) {

            Map<String, Object> map = new HashMap<>();
            map.put("date",orderSetting.getOrderDate().getDate());
            map.put("number",orderSetting.getNumber());
            map.put("reservations",orderSetting.getReservations());
            list.add(map);

        }

        return list;
    }*/
    @Override
    public List<Map> findOrderSettingByMonth(String date) {

        //定义查询的每个月的起始值
        String dateBegin = date+"-01";
        //定义查询的每个月的结束值
        String dateEnd = date+"-31";

        //通过日期查询指定月份的可预约数据
        List<Map> orderSettings = orderSettingMapper.findOrderSettingByMonth(dateBegin,dateEnd);

        return orderSettings;
    }
}
